package com.neusoft.crm.controller;

import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.ISysUserService;
import com.neusoft.crm.utils.EncryptorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统用户管理前端接口
 *
 * @author msprc
 */
@RestController
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 按照账号名和密码查找用户HTTP接口
     *
     * @param sysUser 请求体：带有账号和密码属性的系统用户实体
     * @return 带有完整数据（密码除外）的系统用户实体
     */
    @RequestMapping("/selectSysUserByNameByPass")
    public SysUser selectSysUserByNameByPass(@RequestBody SysUser sysUser) {
        // 对前端传来的对象密码进行加密
        EncryptorUtil encryptor = new EncryptorUtil();
        sysUser.setUserPassword(encryptor.getSha256(sysUser.getUserPassword()));
        return sysUserService.selectSysUserByNameByPass(sysUser);
    }

    /**
     * 查找系统用户列表
     *
     * @param sysUser 请求体：基础用户
     * @return 用户列表
     */
    @RequestMapping("/selectSystemUserList")
    public List<SysUser> selectSystemUserList(@RequestBody SysUser sysUser) {
        return sysUserService.selectSystemUserList(sysUser);
    }

    /**
     * 更新系统用户
     *
     * @param sysUser 请求体：要更新的用户
     * @return 受影响的行数
     */
    @RequestMapping("/updateSystemUserInfo")
    public int updateSystemUserInfo(@RequestBody SysUser sysUser) {
        // 密码发生了更改
        if (sysUserService.selectSysUserByNameByPass(sysUser) == null) {
            EncryptorUtil encryptor = new EncryptorUtil();
            sysUser.setUserPassword(encryptor.getSha256(sysUser.getUserPassword()));
        }
        return sysUserService.updateSystemUserInfo(sysUser);
    }

    /**
     * 新增系统用户
     *
     * @param sysUser 请求体：要新建的用户
     * @return 受影响的行数
     */
    @RequestMapping("/addSystemUserInfo")
    public int addSystemUserInfo(@RequestBody SysUser sysUser) {
        /*
           对前端传来的对象密码进行加密
        */
        EncryptorUtil encryptor = new EncryptorUtil();
        sysUser.setUserPassword(encryptor.getSha256(sysUser.getUserPassword()));
        return sysUserService.addSystemUserInfo(sysUser);
    }

    /**
     * 删除系统用户
     *
     * @param sysUser 请求体：要删除的用户
     * @return 受影响的行数
     */
    @RequestMapping("/deleteSystemUser")
    public int deleteSystemUser(@RequestBody SysUser sysUser) {
        return sysUserService.deleteSystemUser(sysUser);
    }

}
